package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.anhuanjia.tools.eao.bean.EaoPublicityQuestions;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;

/**
 * 公示投诉及意见表操作类
 * 
 * @author Administrator
 *
 */
@Alias(value = "publicityQuestions")
public class PublicityQuestionsUI extends UIFacade {

	/**
	 * 增加或更改
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		String publicityQuestionsList = $V("PublicityQuestionsList");
		if (StringUtil.isNotEmpty(publicityQuestionsList)) {
			Transaction transaction = new Transaction();
			if (StringUtil.isNotEmpty(publicityQuestionsList)) {
				List<Map<String, Object>> array = JSON.parseObject(publicityQuestionsList,
						new TypeReference<List<Map<String, Object>>>() {
						});
				List<String> publicityids = new ArrayList<>();
				String project_id = null;
				for (Map<String, Object> list : array) {
					System.out.println(list);
					EaoPublicityQuestions publicityQuestions = new EaoPublicityQuestions();
					publicityQuestions.setValue(list);
					project_id = publicityQuestions.getProjectId();
					String id = publicityQuestions.getPublicityId();
					if (StringUtils.isEmpty(id)) {
						// 添加
						publicityQuestions.setPublicityId(UUID.randomUUID().toString());
						transaction.insert(publicityQuestions);
						publicityids.add(publicityQuestions.getPublicityId());
					} else {
						// 更新
						publicityids.add(id);
						DataTable dt = new Q(" select * from eao_publicity_questions where Publicityid = ?", id)
								.fetch();
						EaoPublicityQuestions entity = new EaoPublicityQuestions();
						entity.setValue(dt.get(0));
						entity.setValue(list);
						transaction.update(entity);
					}
				}
				DataTable dt = new Q(" select * from eao_publicity_questions where Projectid = ?", project_id).fetch();
				for (DataRow dataRow : dt) {
					EaoPublicityQuestions entity = new EaoPublicityQuestions();
					entity.setValue(dataRow);
					Boolean del = true;
					for (String scid : publicityids) {
						if (entity.getPublicityId().equals(scid)) {
							del = false;
						}
					}
					if (del) {
						transaction.delete(entity);
					}
				}
			}
			if (transaction.commit()) {
				s("操作成功");
			} else {
				f("操作失败");
			}
		} else {
			this.Response.put("data", "无操作记录");
		}
	}

	/**
	 * 根据项目ID查询其下的专家技术评估意见
	 */
	@Priv(loginType = LoginType.Member)
	public void findByProjectId() {
		String pid = $V("Id");
		if (StringUtils.isEmpty(pid)) {
			f("查询失败，请确认查询信息");
			return;
		}
		DataTable dt = new Q("select * from eao_publicity_questions where Publicityid = ? ", pid).fetch();
		List<EaoPublicityQuestions> list = new ArrayList<>();
		if (dt.getRowCount() > 0) {
			for (DataRow dr : dt) {
				EaoPublicityQuestions entity = new EaoPublicityQuestions();
				entity.setValue(dr);
				list.add(entity);
				this.Response.put("data", list);
			}
		}
	}
}
